require 'sanitize'

class Remark
  include Mongoid::Document
  include Mongoid::Timestamps::Created
  include Mongoid::Timestamps::Updated

  belongs_to :topic

  field :content, type: String
  # field :position, type: Integer, default: 0

  belongs_to :author, class_name: "User", inverse_of: :remarks

  belongs_to :parent_remark, class_name: "Remark", inverse_of: :sub_remarks
  has_many :sub_remarks, class_name: "Remark", inverse_of: :parent_remark

  # remark data
  field :remark_type, type: String, default: ''
  has_one :question_version, dependent: :destroy

  alias_method :original_remark_type, :remark_type
  def remark_type
    type = self.original_remark_type
    def type.can_have_question_version?
      self == 'suggestion' || self == 'new'
    end
    type
  end

  def remark_type_allowed?(new_type)
    check_remark_type self, new_type
  end

  # for question topic,
  #   ('new'), 'suggestion', 'confirm'
  #   'new' is not directly available here, but when remark belongs to 
  #   a question_list is moved to question, the type is kept, so it's here
  # for question_list
  #   'new', 'suggestion', 'confirm'
  #   after confirming, all related remarks are moved to the new question
  #   related remarks are all that is replying for the 'new' remark
  #   so 'new' cannot be a sub_remark for any other one
  # for all remarks
  #   '', 'task'

  def get_linked_content
    self.content.gsub(/</, '&lt;').gsub(/>/, '&gt;').gsub(/ /, '&nbsp;')
        .gsub(URI.regexp, '<a href="\0">\0</a>').gsub(/\n/, '<br/>') 
  end

  def parent_question_version
    if self.parent_remark.nil?
      self.topic.question.nil? ? nil : self.topic.question.current_version
    else
      self.parent_remark.question_version.nil? ? 
          self.topic.question.nil? ? nil : self.topic.question.current_version : 
          self.parent_remark.question_version
    end
  end

  def parent_remark_type
    self.parent_remark.nil? ? nil :
      self.parent_remark.remark_type
  end

  def get_question_version
    if self.question_version.nil?
      qv = QuestionVersion.new
      qv.remark = self
      #qv.author = self.author
      qv_original = parent_question_version
      if qv_original.nil?
        qv.question_type = self.topic.get_question_list.section.default_question_type
      else
        qv.question_type = qv_original.question_type
        qv.data = qv_original.data.clone
      end
      self.question_version = qv
    else
      self.question_version
    end
  end
  
  # Notification
  after_create do |doc|
    Notification.for doc do |n|
      n.send doc.parent_remark.author, 'remark_reply' if doc.parent_remark
      n.send doc.topic.author, 'remark_topic' if doc.topic
      n.send doc.topic.get_question_list.author, 'remark_questiontopic' if doc.topic.get_question_list
      # TODO question_list, task
    end
  end

private

  def check_remark_type(remark, new_type)
    if new_type == ''
      true
    elsif new_type == 'suggestion'
      remark.topic.belongs_to_question? || 
      check_remark_type_simple(remark.parent_remark_type, new_type)
    elsif new_type == 'new'
      remark.topic.belongs_to_question_list? ||
      check_remark_type_simple(remark.parent_remark_type, new_type)
    else
      check_remark_type_simple(remark.parent_remark_type, new_type)
    end
  end
  def check_remark_type_simple(parent, child)
    arr = {
      'new' => ['suggestion', 'confirm'],
      'suggestion' => ['suggestion', 'confirm']
    } [parent.nil? ? '' : parent]
    arr.nil? ? child == '' : ( arr.include?(child) || child == '' )
  end
end
